<?php


namespace app\common\service;


use app\common\helper\FormatHelper;
use app\common\model\ActivityItem;
use app\common\model\ActivityItemViper;
use think\Db;

/**
 * 支付回调回写逻辑服务
 *
 * Class ActivityService
 * @package app\common\service
 */
class NotifyService {

    /**
     * 结账支付成功回写订单
     *
     * @param $check_out_no    单据ID
     * @param $order_amt       支付金額
     * @param $transaction_id  支付单号
     * @param $pay_type       订单类型:ALIPAY，WECHAT
     * @return array|void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function paySuccess($check_out_no,$opayment_money,$transaction_id,$pay_type){
        //更新提成记录表
        $check_out = Db::name('qt_check_out')->where([
            'order_no'=>$check_out_no
        ])->find();
        if (empty($check_out)){
            return FormatHelper::fReturn('单据编号错误，没有查询到单据！','',FormatHelper::ERROR);
        }
        //更新单据状态
        $payContent = "";
        if($pay_type == "ALIPAY"){
            $payContent = '[{"pay_id":4,"pay_type":"支付宝","pay_money":'.$opayment_money.'}]';
        }
        if($pay_type == "WECHAT"){
            $payContent = '[{"pay_id":3,"pay_type":"微信","pay_money":'.$opayment_money.'}]';
        }
        $check_out->payment_money = $opayment_money;
        $check_out->pay_status = 1;
        $check_out->pay_content = $payContent;
        $check_out->save();
        //插入支付记录
        if($pay_type == "ALIPAY"){
            $payId = 4;
            $payName = '支付宝';
        }
        if($pay_type == "WECHAT"){
            $payId = 3;
            $payName = '微信';
        }
        $insert['store_id'] = $check_out['store_id'];
        $insert['user_id'] = $check_out['cashier_id'];
        $insert['check_out_id'] = $check_out['id'];
        $insert['pay_id'] = $payId;
        $insert['pay_name'] = $payName;
        $insert['pay_money'] = $opayment_money;
        $insert['type'] = 2;
        $insert['time'] = date('Y-m-d');
        $insert['create_time'] = time();
        Db::name('qt_check_out_pay')->insert($insert);
        //更新商品订单
        Db::name('qt_product_order_item')->where('id',explode(',',$check_out['goods_order']))->update([
            'pay_no'=>$transaction_id,
            'pay_status'=>1,//已支付 支付状态
            'pay_time'=>time()//支付时间
        ]);
        //更新项目
        Db::name('qt_order_project')->where('id',explode(',',$check_out['project_order']))->update([
            'pay_no'=>$transaction_id,
            'pay_status'=>1,//已支付 支付状态
            'pay_time'=>time()//支付时间
        ]);
        return FormatHelper::fReturn('操作成功！','',FormatHelper::SUCCESS);
    }


}